﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_309_13
    {
        public static void ISYC_309_13(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = Convert.ToDouble(data["oal"]) * scale;
            double lpr = Convert.ToDouble(data["lpr"]) * scale;
            double lf = Convert.ToDouble(data["lf"]) * scale;
            double a = Convert.ToDouble(data["a"]);
            double kapr = Convert.ToDouble(data["kapr"]);
            double bd = Convert.ToDouble(data["bd"]) * scale;
            double wf = Convert.ToDouble(data["wf"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;




            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2.5);
            Vector2 v5 = new Vector2(v3.X + 0.9, v3.Y + dconms / 2.227);
            Vector2 v6 = new Vector2(v3.X + oal - lpr, v3.Y + dconms / 2.108);
            Vector2 v7 = new Vector2(v6.X, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v7.X + lpr / 7.341, v7.Y);
            Vector2 v9 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v10 = new Vector2(v7.X, v9.Y);
            Vector2 v11 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v12 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v13 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v14 = new Vector2(v10.X + lf, v2.Y - wf);
            Vector2 v15 = fun.Polar(v14, (180 - kapr - a) * pi / 180, l);
            Vector2 v16 = fun.Polar(v15, (180 - kapr) * pi / 180, l);
            Vector2 v17 = fun.Polar(v14, (180 - kapr) * pi / 180, l);
            Vector2 v18 = fun.Polar(v14, (180 - kapr) * pi / 180, l / 1.54);
            Vector2 v19 = new Vector2(v18.X - lf / 11.623, v1.Y - bd / 2);
            Vector2 v20 = new Vector2(v9.X + lf / 13.523, v19.Y);
            Vector2 v21 = new Vector2(v9.X, v3.Y - dconms / 2.233);
            Vector2 v22 = new Vector2(v21.X, v21.Y * -1);
            Vector2 v23 = new Vector2(v20.X, v20.Y * -1);
            Vector2 v24 = fun.Polar(v15, (180 - kapr) * pi / 180, l / 6.758);
            Vector2 v25 = new Vector2(v3.X + oal, v24.Y + wf / 7.653);
            Vector2 v26 = new Vector2(v25.X, v3.Y + bd / 3.274);
            Vector2 v27 = new Vector2(v26.X - lpr / 7.625, v23.Y);
            Vector2 v28 = new Vector2(v19.X - lf / 18.64, v19.Y);
            Vector2 v29 = new Vector2(v28.X, v27.Y);
            Vector2 v30 = fun.Polar(v14, (180 - kapr - a / 2) * pi / 180, l / 2.617);
            Vector2 v31 = fun.Polar(v30, (180 - kapr - a) * pi / 180, l / 1.757);
            Vector2 v32 = fun.Polar(v31, (180 - kapr) * pi / 180, l / 1.757);
            Vector2 v33 = fun.Polar(v30, (180 - kapr) * pi / 180, l / 1.757);
            Vector2 v34 = new Vector2(v14.X + (v16.X - v14.X) / 2, v14.Y + (v16.Y - v14.Y) / 2);
            Vector2 v35 = new Vector2(v6.X, v3.Y);

            Vector2 v36 = new Vector2(v1.X, v1.Y + oal);
            Vector2 v37 = new Vector2(v2.X, v36.Y);
            Vector2 v38 = new Vector2(v3.X, v36.Y);
            Vector2 v39 = new Vector2(v38.X, v38.Y + dconms / 2.5);
            Vector2 v40 = new Vector2(v5.X, v38.Y + dconms / 2.227);
            Vector2 v41 = new Vector2(v6.X, v38.Y + dconms / 2.108);
            Vector2 v42 = new Vector2(v41.X, v38.Y - dconms / 2.108);
            Vector2 v43 = new Vector2(v40.X, v38.Y - dconms / 2.227);
            Vector2 v44 = new Vector2(v38.X, v38.Y - dconms / 2.5);
            Vector2 v45 = new Vector2(v41.X, v38.Y + dconms / 2);
            Vector2 v46 = new Vector2(v8.X, v45.Y);
            Vector2 v47 = new Vector2(v46.X, v46.Y - dconms);
            Vector2 v48 = new Vector2(v45.X, v47.Y);
            Vector2 v49 = new Vector2(v46.X, v38.Y + dconms / 2.233);
            Vector2 v50 = new Vector2(v23.X, v38.Y + bd / 2);
            Vector2 v51 = new Vector2(v26.X, v38.Y);
            Vector2 v52 = new Vector2(v51.X, v51.Y - bd / 3.179);
            Vector2 v53 = new Vector2(v52.X - lpr / 17.867, v51.Y - bd / 1.687);
            Vector2 v54 = new Vector2(v19.X, v53.Y);
            Vector2 v55 = new Vector2(v54.X, v38.Y - bd / 2);
            Vector2 v56 = new Vector2(v50.X, v55.Y);
            Vector2 v57 = new Vector2(v49.X, v38.Y - dconms / 2.233);
            Vector2 v58 = new Vector2(v54.X, v51.Y);
            Vector2 v59 = new Vector2(v28.X, v58.Y);
            Vector2 v60 = new Vector2(v59.X - lf / 19.157, v59.Y + bd / 5.328);
            Vector2 v61 = new Vector2(v60.X, v50.Y);
            Vector2 v62 = new Vector2(v24.X, v51.Y);
            Vector2 v63 = new Vector2(v62.X, v62.Y - s);
            Vector2 v64 = new Vector2(v15.X, v51.Y);
            Vector2 v65 = new Vector2(v64.X, v63.Y);
            Vector2 v66 = new Vector2(v14.X, v63.Y);
            Vector2 v67 = new Vector2(v66.X, v51.Y);
            Vector2 v68 = new Vector2(v18.X, v66.Y);
            Vector2 v69 = new Vector2(v68.X, v54.Y);
            Vector2 v70 = new Vector2(v45.X, v36.Y);
            Vector2 v71 = new Vector2(v51.X + dconms / 1.5, v51.Y);
            Vector2 v72 = new Vector2(v62.X - l / 3.306, v62.Y);







            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            fun.PolyFill(dxf, line2, 0.2, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            fun.PolyFill(dxf, line3, 0.2, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v23));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v25));
            line4.Vertexes.Add(new LwPolylineVertex(v24));
            line4.Vertexes[0].Bulge = Math.Tan(72 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.2, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v21));
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v18));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v28));
            line5.Vertexes.Add(new LwPolylineVertex(v29));
            line5.Vertexes[0].Bulge = Math.Tan(-72 * pi / 180 / 4);
            line5.Vertexes[5].Bulge = Math.Tan(-59 * pi / 180 / 4);
            fun.PolyFill(dxf, line5, 0.2, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v14));
            line6.Vertexes.Add(new LwPolylineVertex(v15));
            line6.Vertexes.Add(new LwPolylineVertex(v16));
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line6, 0.2, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v30));
            line7.Vertexes.Add(new LwPolylineVertex(v31));
            line7.Vertexes.Add(new LwPolylineVertex(v32));
            line7.Vertexes.Add(new LwPolylineVertex(v33));
            line7.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, line7, 0.2, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v37));
            line12.Vertexes.Add(new LwPolylineVertex(v71));
            fun.PolyFill(dxf, line12, 0.03, dimensioning);




            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            fun.dimrot1(dxf, v7, v10, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v16, v15, -90, dconms / 2, 1, height, arrowsize, exline, "L", "", "");

            fun.dimrot1(dxf, v2, v14, -90, dconms, 1, height, arrowsize, exline, "WF", "", "");

            fun.dimrot1(dxf, v23, v20, -90, dconms / 2, 1, height, arrowsize, exline, "BDRED", "", "");

            fun.dimrot1(dxf, v14, v10, 180, dconms * 1.5, 1, height, arrowsize, exline, "LF", "", "");

            fun.dimrot1(dxf, v14, v10, 180, dconms, 1, height, arrowsize, exline, "LDRED", "", "");

            fun.dimrot1(dxf, v29, v26, 0, dconms, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, v10, v25, 180, dconms * 2, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v13, v25, 180, dconms * 2.5, 1, height, arrowsize, exline, "OAL", "", "");

            fun.jdbz2(dxf, new Line(v14, fun.Polar(v14, (180 - kapr) * pi / 180, 0.1)), new Line(v14, fun.Polar(v14, 180 * pi / 180, 0.1)), l * 1.3, height, arrowsize, exline, "KAPR", "", "");

            fun.jt(dxf, new Vector2(v5.X + (v6.X - v5.X) / 2, v5.Y + (v6.Y - v5.Y) / 2), 135, 9, height, "CCTMS,CCFMS,CZCMS");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }







            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}